package com.bestvike.stone.core.collection;

import java.util.Objects;

/**
 * Created by 许崇雷 on 2019-01-04.
 */
public final class KeyedCollectionUtils {
    /**
     * 判断两个 KeyedCollection 是否相等
     *
     * @param left    被比较值
     * @param right   比较值
     * @param <TKey>  键类型
     * @param <TItem> 值类型
     * @return 相等返回 true, 否则返回 false
     */
    public static <TKey, TItem> boolean equals(KeyedCollection<TKey, TItem> left, KeyedCollection<TKey, TItem> right) {
        if (left == right)
            return true;

        if (left == null || right == null || left.size() != right.size())
            return false;

        for (TItem leftItem : left) {
            TItem rightItem = right.get(left.getKeyForItem(leftItem));
            if (!Objects.equals(leftItem, rightItem))
                return false;
        }
        return true;
    }
}
